package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import ve.com.wayoyo.wuml_case.R;
import ve.com.wayoyo.wuml_case.UserPref;
import ve.com.wayoyo.wuml_case.Exceptions.ElementosAtributosException;
import android.R.color;
import android.R.integer;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class AtributoComponente extends Dialog{

	EditText etName;
	private boolean atr_final;
	private boolean atr_static;
	private boolean atr_volatile;
	private boolean ismultiplicity;
	private String modificador;
	private String nombreVariable;
	private String tipoDato;
	private String leftMultiplicity;
	private String rightMultiplicity;
	private int multiplicityType;

	private EditText etSingleNum;
	private EditText etNumAsterisco;
	private EditText etNumNumFirst;
	private EditText etNumNumLast;


	public AtributoComponente(Context context) {
		super(context);
	}

	public void alert(String mensaje)
	{
		Toast.makeText(this.getContext(), mensaje , Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.atributo_componente_layout);
		int pageWidth = UserPref.getInstancia().getWinWidth();
		int pageHeight = UserPref.getInstancia().getWinHeight();
		LayoutParams params = getWindow().getAttributes();
		if (pageWidth<410){
			params.width = pageWidth;
			params.height = pageHeight;
		}else{
			params.width = 410;
			params.height = 350;   
		}		     
		getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
		setTitle("Nuevo Atributo");
		atr_final = false;
		atr_static = false;
		atr_volatile = false;
		ismultiplicity = false;
		this.setModificadores();		
		this.setCardinality();
		this.setok();
	}

	private void setCardinality() {
		final CheckBox multiplicity = (CheckBox)findViewById(R.id.multiplicityAtr);		
		final RadioButton rbSingleNum = (RadioButton)findViewById(R.id.rb_single_numb);
		final RadioButton rbNumbAsterisco = (RadioButton)findViewById(R.id.rb_numb_asterisco);
		final RadioButton rbNumbNumb = (RadioButton)findViewById(R.id.rb_numb_numb);

		etSingleNum = (EditText)findViewById(R.id.et_single_numb);
		etNumAsterisco = (EditText)findViewById(R.id.et_numb_asterisco);
		etNumNumFirst = (EditText)findViewById(R.id.et_numb_numb_first);
		etNumNumLast = (EditText)findViewById(R.id.et_numb_numb_last);
		etNumNumFirst.setTextColor(Color.BLACK);
		etNumNumLast.setTextColor(Color.BLACK);

		multiplicity.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					LinearLayout ln = (LinearLayout)findViewById(R.id.globalLayout_multiplicity);
					ln.setVisibility(View.VISIBLE);
					ismultiplicity = true;
					//		        	singlenum.setEnabled(true);
					//		        	txsinglenum.setText("]");
					//		        	txsinglenum.setTextSize(16);		        	
				}
				else {
					LinearLayout ln = (LinearLayout)findViewById(R.id.globalLayout_multiplicity);
					ln.setVisibility(View.GONE);
					ismultiplicity = false;
					//		        	singlenum.setEnabled(false);
					rbSingleNum.setChecked(false);
					rbNumbAsterisco.setChecked(false);
					rbNumbNumb.setChecked(false);
					//		        	etsinglenum.setEnabled(falsce);
					//		        	etsinglenum.setText("");
					//		        	txsinglenum.setText("");
					//		        	txsinglenum.setHint("]");
				}
			}
		});

		RadioGroup rg_cardinality = (RadioGroup)findViewById(R.id.rgCardinality);
		modificador = "default";
		rg_cardinality.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.rb_single_numb){
					etSingleNum.setEnabled(true);
					etNumAsterisco.setEnabled(false);
					etNumAsterisco.setText("");
					etNumNumFirst.setEnabled(false);
					etNumNumFirst.setText("");
					etNumNumLast.setEnabled(false);
					etNumNumLast.setText("");
					multiplicityType = 0;

				}else if(checkedId == R.id.rb_numb_asterisco){
					etSingleNum.setEnabled(false);
					etSingleNum.setText("");
					etNumAsterisco.setEnabled(true);
					etNumNumFirst.setEnabled(false);
					etNumNumFirst.setText("");
					etNumNumLast.setEnabled(false);
					etNumNumLast.setText("");
					multiplicityType = 1;

				}else if (checkedId == R.id.rb_numb_numb){
					etSingleNum.setEnabled(false);
					etSingleNum.setText("");
					etNumAsterisco.setEnabled(false);
					etNumAsterisco.setText("");
					etNumNumFirst.setEnabled(true);
					etNumNumLast.setEnabled(true);
					multiplicityType = 2;
				}

			}
		});
	}

	public void setModificadores()
	{
		final CheckBox cb_final = (CheckBox)findViewById(R.id.modif_final);
		final CheckBox cb_volatile = (CheckBox)findViewById(R.id.modif_volatile);
		CheckBox cb_static = (CheckBox)findViewById(R.id.modif_static);
		cb_final.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					atr_volatile = false;
					cb_volatile.setChecked(false);
					atr_final = true;
				}
				else {
					atr_final = false;
				}
			}
		});

		cb_static.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					atr_static = true;
				}
				else {
					atr_static = false;
				}
			}
		});

		cb_volatile.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					atr_final = false;
					cb_final.setChecked(false);
					atr_volatile = true;
				}
				else {
					atr_volatile = false;
				}
			}
		});

		RadioGroup rg_alcance = (RadioGroup)findViewById(R.id.alcane_atributo);
		modificador = "default";
		rg_alcance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.radio_button_public)
					modificador = "public";
				else if (checkedId == R.id.radio_button_private)
					modificador = "private";
				else if (checkedId == R.id.radio_button_protected)
					modificador = "protected";
				else
					modificador = "default";
			}
		});
	}

	public void setok(){
		Button buttonOK = (Button) findViewById(R.id.btAtributo);
		buttonOK.setText("Agregar");
		buttonOK.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				try{
					nombreVariable = ((EditText) findViewById(R.id.nombreAtributoNuevo)).getText().toString();
					tipoDato = ((EditText) findViewById(R.id.nuevoTipoDato)).getText().toString();
					if ((nombreVariable.equals(""))||(tipoDato.equals("")))
						throw new ElementosAtributosException();
					VistaDiagrama vista = VistaDiagrama.getVista();
					CompAtributo comp = new CompAtributo(nombreVariable,modificador.toLowerCase(),tipoDato);
					comp.setFinal(atr_final);
					comp.setStatic(atr_static);
					comp.setVolatile(atr_volatile);
					comp.set_haveMultiplicity(ismultiplicity);
					if (ismultiplicity)
						switch (multiplicityType) {
						case 0:
							comp.set_leftMultiplicity(String.valueOf(etSingleNum.getText()));
							comp.set_rightMultiplicity(String.valueOf(etSingleNum.getText()));					
							break;
						case 1:
							comp.set_leftMultiplicity(String.valueOf(etNumAsterisco.getText()));
							comp.set_rightMultiplicity("*");					
							break;
						case 2:
							comp.set_leftMultiplicity(String.valueOf(etNumNumFirst.getText()));
							comp.set_rightMultiplicity(String.valueOf(etNumNumLast.getText()));
							break;
						}				

					Diagrama diag = vista.getDiagrama();
					((DiagramaClases) diag).agregarAtributo(comp);				
					vista.invalidate();
					AtributoComponente.this.dismiss();
				}catch (ElementosAtributosException ex){
					alert(ex.getMessage());
				}catch (Exception e) {
					etNumNumFirst.setTextColor(Color.RED);
					etNumNumLast.setTextColor(Color.RED);
					alert(e.getMessage());
				}
			}
		});

	}
}